#!/bin/tcsh -ef
#############################################################################
#                                                                           #
# Title: Prepare Large Images (experimental)                                #
#                                                                           #
# (C) 2dx.org, GNU Plublic License.                                         #
#                                                                           #
# Created..........: 03/01/2006                                             #
# Last Modification: 03/01/2006                                             #
# Author...........: 2dx.org                                                #
#                                                                           #
#############################################################################
#
# SORTORDER: 65
#
# MANUAL: This script will take large TIFF images in a given directory and transform them into smaller MRC files.
# 
# MANUAL: This will be done by cropping the TIFF images of any size into several square sub-images of reasonable sizes.
#
# MANUAL: The created sub-images will be in MRC file format, and have then to be "imported" with the 2dx_merge function.
#
# DISPLAY: airport_dir
# DISPLAY: airport_size
# DISPLAY: airport_OD
# DISPLAY: airport_delete
#
#$end_local_vars
#
set bin_2dx = ""
set proc_2dx = ""
set app_2dx_image = ""
#
set airport_dir = ""
set airport_size = ""
set airport_OD = ""
set airport_delete = ""
#
#$end_vars
#
set scriptname = 2dx_import_large
\rm -f LOGS/${scriptname}.results
#
set ccp4_setup = 'y'
source ${proc_2dx}/initialize
#
echo airport_dir = ${airport_dir}
echo airport_size = ${airport_size}
echo airport_OD = ${airport_OD}
echo airport_delete = ${airport_delete}
#
echo "<<@progress: 1>>"
#
set olddir = $PWD
#
cd ${airport_dir}
#
echo "::Scanning for *.tif files in ${airport_dir}"
#
echo dummy > dummy.tif
ls -1 *.tif > tmp.tmp
set isthere = `wc -l tmp.tmp | cut -f1 | awk '{ if ( $1 > 1 ) { s = 1 } else { s = 2 }} END { print s }'`
set number = `wc -l tmp.tmp | cut -f1 | awk '{ s = $1 - 1 } END { print s }'`
\rm -f dummy.tif tmp.tmp
#
if ( ${isthere} == "2" ) then
  ${proc_2dx}/protest "ERROR: No *.tif files found here."
else
  ${proc_2dx}/linblock "Found ${number} of *.tif files."
  set progress_increment = `echo ${number} | awk '{ s = int(80 / $1) } END { print s }'`
endif
#
foreach largefile ( *.tif )
  #
  ${proc_2dx}/linblock "Preparing ${largefile}"
  #
  set namecore = `echo ${largefile} | cut -d\. -f1`
  if ( ! -e ${namecore}.tif ) then
    ${proc_2dx}/linblock "ERROR with filename ${largefile}:"
    ${proc_2dx}/linblock "ERROR: ${namecore}.tif not found."
    ${proc_2dx}/protest "ERROR: Only one . (dot) allowed in file name."
  endif
  #
  if ( ${airport_OD} == "yes" ) then 
    #
    ${proc_2dx}/lin "Converting to ${namecore}-tmp.mrc"
    #
    \rm -f ${namecore}-tmp.mrc
    #
    ${bin_2dx}/tif2mrc.exe << eot
${namecore}.tif
${namecore}-tmp.mrc
y
eot
    #
    ${proc_2dx}/lin "Converting to OD file ${namecore}.mrc"
    #
    \rm -f ${namecore}.mrc
    #
    ${bin_2dx}/labelh.exe << eot
${namecore}-tmp.mrc
10
${namecore}.mrc
eot
    #
    \rm -f ${namecore}-tmp.mrc
  else
    #
    ${proc_2dx}/lin "Converting to ${namecore}.mrc"
    #
    \rm -f ${namecore}.mrc
    #
    ${bin_2dx}/tif2mrc.exe << eot
${namecore}.tif
${namecore}.mrc
y
eot
    #
  endif
  #
  ${proc_2dx}/lin "Cropping into small files"
  #
  echo dummy > ${namecore}01.mrc
  \rm -f ${namecore}??.mrc
  #
  ${bin_2dx}/2dx_prep.exe << eot
${namecore}
${airport_size}
eot
  #
  if ( -e ${namecore}01.mrc ) then
    if ( ${airport_delete} == "yes" ) then
      ${proc_2dx}/lin "Removing ${namecore}.tif"
      \rm -f ${namecore}.tif
    endif
    \rm -f ${namecore}.mrc
    if ( ! -d MRC-SUBIMAGES ) then
      \mkdir MRC-SUBIMAGES 
    endif
    foreach subimage ( ${namecore}??.mrc )
      \mv -f ${subimage} MRC-SUBIMAGES
      echo "# IMAGE-IMPORTANT: ${airport_dir}/MRC-SUBIMAGES/${subimage} <MRC: ${subimage}>" >> ${olddir}/LOGS/${scriptname}.results
    end
    ${proc_2dx}/linblock "Done. See in ${airport_dir}/MRC-SUBIMAGES"
    #
  else
    ${proc_2dx}/linblock "ERROR while processing ${namecore}.tif"
  endif
  #
  echo "<<@progress: +${progress_increment}>>"
  #
end
#
echo "<<@progress: 100>>"
#
${proc_2dx}/linblock "${scriptname} finished correctly."
#
